<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 22:42:45 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1240 $
 * $Author: gavinspearhead $
 * $Id: ajax_edittransfers.php 1240 2008-06-29 20:42:45Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);
$__auth = 'silent';

$pathaet = realpath(dirname(__FILE__));
require_once "$pathaet/../functions/functions.php";
require_once "$pathaet/../functions/html_includes.php";


if (!$isadmin)
	die_html($LN['error_noadmin']);

$cmd = isset($_REQUEST['cmd'])?$_REQUEST['cmd']:'';
$id = isset($_REQUEST['id'])?$_REQUEST['id']:'';

$db->escape($cmd);
$db->escape($id);

$prefs = load_config($db);

switch (strtolower($cmd)) {
case 'delete' :
	verify_challenge($_POST['challenge']);
	if (is_numeric($id) && $_SERVER['REQUEST_METHOD'] == 'POST') {
		delete_rss_feed($db, $id);
		$uprefs = load_config($db);
		$uc = new urdd_client($uprefs['urdd_host'], $uprefs['urdd_port'], $userID);
		$uc->cancel(get_command(COMMAND_UPDATE_RSS) . " $id");
		$uc->cancel(get_command(COMMAND_EXPIRE_RSS) . " $id");
		$uc->unsubscribe($id);
		remove_schedule($db, $uc, $id, COMMAND_UPDATE_RSS);
		$uc->disconnect();

	} else
		die_html ('Unknown ID');
	break;
case 'showeditrss':
	// Get download info:
	if (is_numeric($id)) {
		$sql = "SELECT * FROM rss_urls WHERE \"id\" = '$id'";
		$res = $db->execute_query($sql);
		$oldname = $res[0]['name'];
		$oldurl = $res[0]['url'];
		$oldusername = $res[0]['username'];
		$oldpassword = $res[0]['password'];
		$oldsubscribed = $res[0]['subscribed'];

		$oldexpire = $res[0]['expire'];
	} else if ($id == 'new') {
		$oldname = $oldurl = $oldpassword = $oldusername = '';
		$oldexpire =  get_config($db, 'default_expire_time');
		$oldsubscribed = RSS_SUBSCRIBED;
	} else
		die_html ('Unknown ID');

	$challenge = set_challenge();
	$smarty->assign('id',		$id);
	$smarty->assign('oldname',	$oldname);
	$smarty->assign('oldpassword',	$oldpassword);
	$smarty->assign('oldusername',	$oldusername);
	$smarty->assign('challenge',   	$challenge );
	$smarty->assign('oldurl',	$oldurl);
	$smarty->assign('oldsubscribed',	$oldsubscribed);
	$smarty->assign('oldexpire',	$oldexpire);
	$smarty->display('ajax_editrss.tpl');
	die();
	break;
case 'update_rss':
	// Actually rename the download
	verify_challenge($_POST['challenge']);
	$newname = trim($_POST['rss_name']);
	$newurl = trim($_POST['rss_url']);
	$username = trim($_POST['rss_username']);
	$password = trim($_POST['rss_password']);
	$newexpire = trim($_POST['rss_expire']);
	$newsubscribed = (isset ($_POST['rss_subscribed']) &&  $_POST['rss_subscribed'] == 'on') ? RSS_SUBSCRIBED : RSS_UNSUBSCRIBED;
	if ($newsubscribed == RSS_UNSUBSCRIBED) {
		$sql = "UPDATE rss_urls SET \"refresh_time\"='0', \"refresh_period\"='0' WHERE \"id\"=$id";
		$db->execute_query($sql);

		$uprefs = load_config($db);
		$uc = new urdd_client($uprefs['urdd_host'], $uprefs['urdd_port'], $userID);
		$uc->cancel(get_command(COMMAND_UPDATE_RSS) . " $id");
		$uc->cancel(get_command(COMMAND_EXPIRE_RSS) . " $id");
		$uc->unsubscribe_rss($id);
		remove_rss_schedule($db, $uc, $id, COMMAND_UPDATE_RSS );
	}
	if (is_numeric($id)) {
		update_rss_url($db, $newname, $newurl, $newsubscribed, $newexpire, $username, $password, $id);
	} else if ($id == 'new') {
		add_rss_url($db, $newname, $newurl, $newsubscribed, $newexpire, $username, $password);
	} else
		die_html ('Unknown ID');
	break;
default:	
	die_html('Invalid command!');
	break;	
}


// Success:
echo 'OK';
?>
